﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StandAlone
{
    public partial class frm_Usuarios_Privilegios : Form
    {
        bool flag = false;
        public static string Usr;

        public frm_Usuarios_Privilegios()
        {
            InitializeComponent();
        }

        private void frm_Usuarios_Privilegios_Load(object sender, EventArgs e)
        {
            label1.Text = Usr;
            Cargar_Lst();
        }
        
        private void Cargar_Lst()
        {
            string Consulta = "SELECT Menues.Menu AS Menu, COUNT(Usuarios_x_Menues.Usr) AS Existe"   
            +" FROM Menues INNER JOIN Usuarios_x_Menues ON Menues.Id_Menu = Usuarios_x_Menues.Id_Menu"
            +" WHERE Usr = " + "'"+ Usr + "'" +" GROUP BY Menu UNION ALL SELECT Menu, 0 as Existe"
            +" FROM Menues WHERE id_Menu not in"
            + " (SELECT id_Menu FROM Usuarios_x_Menues WHERE Usr= " + "'" + Usr + "'" + " ) order by Menu";
            
            Funciones.Cargar_Lst(Consulta, "Menu", "Existe", ref lst_Menues);
        }

        private void lst_Menues_SelectedIndexChanged(object sender, EventArgs e)
        {
            flag = true;
            if (lst_Menues.SelectedValue.ToString() == (1).ToString())
            {
                rb_Autorizado.Checked = true;
            }
            else
            {
                rb_NoAutorizado.Checked = true;
            }
            flag = false;
        }

        private void rb_Autorizado_CheckedChanged(object sender, EventArgs e)
        {
            if (flag == false)
            {
                if (rb_Autorizado.Checked == true)
                {
                    string id_menu = Buscar_Codigo_Menu();
                    //Agregar Registro.
                    string Consulta = "INSERT INTO Usuarios_x_Menues (Usr, Id_Menu)"
                        + " SELECT '" + Usr + "', '" + id_menu + "'";
                    Funciones.Insert_DataBase(Consulta);
                    MessageBox.Show("Proveedor Agregado con Éxito", "Agregar Nuevo Proveedor", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                else
                {
                    //Borrar Registro.
                    string id_menu = Buscar_Codigo_Menu();
                    string Consulta = "DELETE FROM Usuarios_x_Menues WHERE Usr = '" + Usr + "' AND Id_Menu = '" + id_menu + "'";
                    Funciones.Insert_DataBase(Consulta);
                    MessageBox.Show("Proveedor Removido con Éxito", "Remover Proveedor", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                Cargar_Lst();
            }
        }
        private string Buscar_Codigo_Menu()
        {
            string Consulta;
            Consulta = "SELECT Menues.Id_Menu as ID FROM Menues WHERE Menues.Menu = '" + lst_Menues.Text + "'";
            DataSet ds = new DataSet();
            ds = Funciones.Read_DataBase(Consulta);
            return ds.Tables[0].Rows[0]["ID"].ToString();
        }

        private void btn_Cerrar_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
